package com.hhit.bigscreenbackmanage.common;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * @ClassName SignatureUtil
 * @Description 签名工具类
 * @Author qianchengcheng
 * @Date 2020/6/24
 */
public class SignatureUtil {

    private static Logger log = LoggerFactory.getLogger(SignatureUtil.class);

    public static String encryptSHA(String method, String resource, String systemCode, long utcTime, int nonce, String secretKey) throws NoSuchAlgorithmException {
        StringBuilder srcStr = new StringBuilder();
        srcStr.append(method);
        srcStr.append("/");
        srcStr.append(resource);
        srcStr.append("?systemCode=");
        srcStr.append(systemCode);
        srcStr.append("&timestamp=");
        srcStr.append(utcTime);
        srcStr.append("&nonce=");
        srcStr.append(nonce);
//        log.info("encryptSHA==>加密参数为:{}",srcStr.toString());

        MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
        mDigest.update(secretKey.getBytes());
        byte[] result = mDigest.digest(srcStr.toString().getBytes());
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < result.length; i++) {
            sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
        }
        return sb.toString();
    }
}
